//
//  CountdownView.m
//  Time
//
//  Created by bangyibang on 14-5-16.
//  Copyright (c) 2014年 bangyibang. All rights reserved.
//

#import "CountdownView.h"

@interface CountdownView()
{
    UILabel *_point1;
    UILabel *_point2;
    UILabel *_point3;
}
@end

@implementation CountdownView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}

- (void)_initSubview
{
    switch (self.timeType) {
        case 0:
        {
//            NSLog(@"self.frame.size.width== %f",self.frame.size.width);
//            if (self.frame.size.width >75) {
//                NSLog(@"ssssssssss");
//            }
            self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y, self.frame.size.width, 30);
//            self.backgroundColor = [UIColor clearColor];
            
            CGFloat hourW = 15 + self.frame.size.width - 75;
            
            self.hourLabel = [[UILabel alloc]initWithFrame:CGRectMake(5, 5, hourW, 20)];
            self.hourLabel.backgroundColor = self.backColor;
            self.hourLabel.textColor = self.textColor;
            self.hourLabel.layer.cornerRadius = 2.0;
            self.hourLabel.layer.masksToBounds = YES;
            self.hourLabel.textAlignment = NSTextAlignmentCenter;
            self.hourLabel.font = FONT(11);
            [self addSubview:self.hourLabel];
            
            _point1 = [[UILabel alloc]initWithFrame:CGRectMake(GET_X(self.hourLabel), 5, 10, 20)];
            _point1.backgroundColor = [UIColor clearColor];
            _point1.text = @":";
            _point1.textAlignment = NSTextAlignmentCenter;
            _point1.font = FONT(11);
            _point1.textColor = self.pointColor;
            [self addSubview:_point1];
            
            self.minsLabel = [[UILabel alloc]initWithFrame:CGRectMake(GET_X(_point1), 5, 15, 20)];
            self.minsLabel.backgroundColor = self.backColor;
            self.minsLabel.textColor = self.textColor;
            self.minsLabel.layer.cornerRadius = 2.0;
            self.minsLabel.layer.masksToBounds = YES;
            self.minsLabel.textAlignment = NSTextAlignmentCenter;
            self.minsLabel.font = FONT(11);
            [self addSubview:self.minsLabel];
            
            _point2 = [[UILabel alloc]initWithFrame:CGRectMake(GET_X(self.minsLabel), 5, 10, 20)];
            _point2.backgroundColor = [UIColor clearColor];
            _point2.text = @":";
            _point2.font = FONT(11);
            _point2.textAlignment = NSTextAlignmentCenter;
            _point2.textColor = self.pointColor;
            [self addSubview:_point2];
            
            self.secsLabel = [[UILabel alloc]initWithFrame:CGRectMake(GET_X(_point2), 5, 15, 20)];
            self.secsLabel.backgroundColor = self.backColor;
            self.secsLabel.textColor = self.textColor;
            self.secsLabel.layer.cornerRadius = 2.0;
            self.secsLabel.layer.masksToBounds = YES;
            self.secsLabel.textAlignment = NSTextAlignmentCenter;
            self.secsLabel.font = FONT(11);
            [self addSubview:self.secsLabel];
            
            break;
        }
        case 1:{
            self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y, 100, 40);
            self.backgroundColor = [UIColor clearColor];
            
            self.minsLabel = [[UILabel alloc]initWithFrame:CGRectMake(5, 5, 40, 30)];
            self.minsLabel.backgroundColor = self.backColor;
            self.minsLabel.textColor = self.textColor;
            self.minsLabel.layer.cornerRadius = 7.0;
            self.minsLabel.textAlignment = NSTextAlignmentCenter;
            self.minsLabel.font = [UIFont systemFontOfSize:24];
            [self addSubview:self.minsLabel];
            
            UILabel *point1 = [[UILabel alloc]initWithFrame:CGRectMake(45, 5, 10, 30)];
            point1.backgroundColor = [UIColor clearColor];
            point1.text = @":";
            point1.textAlignment = NSTextAlignmentCenter;
            point1.textColor = self.pointColor;
            [self addSubview:point1];
            
            self.secsLabel = [[UILabel alloc]initWithFrame:CGRectMake(55, 5, 40, 30)];
            self.secsLabel.backgroundColor = self.backColor;
            self.secsLabel.textColor = self.textColor;
            self.secsLabel.layer.cornerRadius = 7.0;
            self.secsLabel.textAlignment = NSTextAlignmentCenter;
            self.secsLabel.font = [UIFont systemFontOfSize:24];
            [self addSubview:self.secsLabel];
            break;
        }
            
        default:
            break;
    }

}

- (void)countdownViewSetStartTime:(NSString *)timeString
{
    [self _initSubview];
//    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
//	[dateFormatter setDateFormat:@"HH:mm:ss"];
//    
//	NSString *timeString = [dateFormatter stringFromDate:date];
//    NSLog(@" = %@",timeString);
    
#warning 需改 当
    if (timeString.length==9) {
        if (self.timeType == 0) {
            
            self.hourLabel.text = [timeString substringWithRange:NSMakeRange(0, 3)];
        }
        self.minsLabel.text = [timeString substringWithRange:NSMakeRange(4, 2)];
        self.secsLabel.text = [timeString substringWithRange:NSMakeRange(7, 2)];
        
        if (self.hourLabel.frame.size.width == 15 ) {
            [self changeSelfFrame];
        }
        
    }else{
        if (self.timeType == 0) {
            
            self.hourLabel.text = [timeString substringWithRange:NSMakeRange(0, 2)];
        }
        self.minsLabel.text = [timeString substringWithRange:NSMakeRange(3, 2)];
        self.secsLabel.text = [timeString substringWithRange:NSMakeRange(6, 2)];
    }

    _time = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(countDownTime:) userInfo:nil repeats:YES];
    [[NSRunLoop mainRunLoop] addTimer:_time forMode:NSRunLoopCommonModes];
}

- (void)countDownTime:(NSTimer *)sender
{
    if ([self.hourLabel.text intValue] == 0 && [self.minsLabel.text intValue] == 0 && [self.secsLabel.text intValue] == 0) {
        
        [_time invalidate];
        self.hourLabel.text = @"00";
        self.minsLabel.text = @"00";
        self.secsLabel.text = @"00";
        
        POST_NOTIFICATION(@"timerFinished", nil);
        return;
    }
    
    int sec = 0;
    int min = 0;
    int hour = 0;
    // 秒数的递减
    if ([self.secsLabel.text intValue] == 0) {
        sec = 60;
    }else{
        sec = [self.secsLabel.text intValue];
    }
    sec--;
    self.secsLabel.text = [NSString stringWithFormat:@"%02d",sec];
    
    // 分钟的递减
    if (sec == 59) {
        if ([self.minsLabel.text intValue] == 0) {
            min = 60;
        }else{
            min = [self.minsLabel.text intValue];
        }
        min--;
        self.minsLabel.text = [NSString stringWithFormat:@"%02d",min];
    }
    
    // 小时的递减
    if (min == 59) {
        if ([self.hourLabel.text intValue] == 0) {
            hour = 60;

        }else{
            hour = [self.hourLabel.text intValue];
        }
        hour--;
        self.hourLabel.text = [NSString stringWithFormat:@"%03d",hour];
    }
    
}

-(void)changeSelfFrame{
//    self.frame = CGRectMake(SCREEN_WIDTH-85-5, 0, 85, 30);
    self.hourLabel.frame = CGRectMake(5, 5, 25, 20);
    _point1.frame = CGRectMake(GET_X(self.hourLabel), 5, 10, 20);
    self.minsLabel.frame = CGRectMake(GET_X(_point1), 5, 15, 20);
    _point2.frame = CGRectMake(GET_X(self.minsLabel), 5, 10, 20);
    self.secsLabel.frame = CGRectMake(GET_X(_point2), 5, 15, 20);
}

- (void)stopTimer
{
    [_time invalidate];
}

- (void)startTimer
{
    [_time isValid];
}



/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

@end
